--[[
	GMod Last Man Standing, a gamemode for Garry's Mod
    Copyright (C) 2012 Wickex

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
]]

local function clr( color ) return color.r, color.g, color.b, color.a; end

glms_hud = { };

function glms_hud:PaintPanel( x, y, w, h, colors )
	surface.SetDrawColor( clr( colors.border ) );
	surface.DrawOutlinedRect( x, y, w, h );
 
	x = x + 1;
	y = y + 1;
	w = w - 2;
	h = h - 2;
 
	surface.SetDrawColor( clr( colors.background ) );
	surface.DrawRect( x, y, w, h );
end

function glms_hud:PaintBar( x, y, w, h, colors, value )
	self:PaintPanel( x, y, w, h, colors );
 
	x = x + 1; y = y + 1;
	w = w - 2; h = h - 2;
 
	local width = w * math.Clamp( value, 0, 1 );
	local shade = 4;
	
	surface.SetDrawColor( clr( colors.shade ) );
	surface.DrawRect( x, y, width, shade );
 
	surface.SetDrawColor( clr( colors.fill ) );
	surface.DrawRect( x, y + shade, width, h - shade );
end

function glms_hud:PaintText( x, y, text, font, colors )
	surface.SetFont( font );
 
	surface.SetTextPos( x + 1, y + 1 );
	surface.SetTextColor( clr( colors.shadow ) );
	surface.DrawText( text );
 
	surface.SetTextPos( x, y );
	surface.SetTextColor( clr( colors.text ) );
	surface.DrawText( text );
end

function glms_hud:TextSize( text, font )
	surface.SetFont( font );
	return surface.GetTextSize( text );
end

local vars =
{
	font = "TargetID",
 
	padding = 10,
	margin = 35,
 
	text_spacing = 2,
	bar_spacing = 5,
 
	bar_height = 20,
 
	width = 0.15
};

local colors =
{
	background =
	{
		border = Color( 255, 190, 128, 255 ),
		background = Color( 240, 120, 0, 75 )
	},
	text =
	{
		shadow = Color( 0, 0, 0, 200 ),
		text = Color( 255, 255, 255, 255 )
	},
	health_bar =
	{
		border = Color( 255, 0, 0, 255 ),
		background = Color( 255, 0, 0, 100 ),
		shade = Color( 80, 255, 80, 255 ),
		fill = Color( 0, 200, 0, 255 )
	},
	hunger_bar =
	{
		border = Color( 255, 128, 0, 255 ),
		background = Color( 255, 128, 64, 100 ),
		shade = Color( 255, 255, 108, 255 ),
		fill = Color( 255, 242, 0, 255 )
	}
};

function GM:HUDPaint()
	local client = client or LocalPlayer()

	if(!client:Alive()) then return end
   
	local _, th = glms_hud:TextSize("TEXT", vars.font)

	local i = 3
	
	local width = vars.width * ScrW()
	local bar_width = width - (vars.padding * i)
	local height = (vars.padding * i) + (th * i) + (vars.text_spacing * i) + (vars.bar_height * i) + vars.bar_spacing
 
	local x = vars.margin
	local y = ScrH() - vars.margin - height
 
	local cx = x + vars.padding
	local cy = y + vars.padding
 
	glms_hud:PaintPanel( x, y, width, height, colors.background )
	
	--[[local by = th + vars.text_spacing
	
	local text = string.format( "Health: %iHP", client:Health( ) );
	glms_hud:PaintText( cx, cy, text, vars.font, colors.text );
	glms_hud:PaintBar( cx, cy + by, bar_width, vars.bar_height, colors.health_bar, client:Health( ) / 100 );
	
	by = by + vars.bar_height + vars.bar_spacing;
 
	local text = string.format( "Hunger: %iHP", client:Health() );
	glms_hud:PaintText( cx, cy + by, text, vars.font, colors.text );
	glms_hud:PaintBar( cx, cy + by + th + vars.text_spacing, bar_width, vars.bar_height, colors.hunger_bar, client:Health( ) / 100 );]]
	
	local by = th + vars.text_spacing
	
	local endtime = GetGlobalFloat("glms_round_end", 0) - CurTime()
	
	local text = string.FormattedTime(math.max(0, endtime), "%02i:%02i")
	glms_hud:PaintText( cx, cy, text, vars.font, colors.text );
	
	by = by + vars.bar_height + vars.bar_spacing
	
	local text = string.format( "Health: %iHP", client:Health( ) )
	glms_hud:PaintText( cx, cy + by, text, vars.font, colors.text )
	--glms_hud:PaintBar( cx, cy + by, bar_width, vars.bar_height, colors.health_bar, client:Health( ) / 100 )
	glms_hud:PaintBar( cx, cy + by + th + vars.text_spacing, bar_width, vars.bar_height, colors.health_bar, client:Health( ) / 100 )
	
	by = by + vars.bar_height + vars.bar_spacing
 
	local text = string.format( "Hunger: %i", client:Health() )
	glms_hud:PaintText( cx, cy + by, text, vars.font, colors.text )
	glms_hud:PaintBar( cx, cy + by + th + vars.text_spacing, bar_width, vars.bar_height, colors.hunger_bar, client:Health( ) / 100 )
end

local hud = {"CHudHealth", "CHudBattery", "CHudAmmo", "CHudSecondaryAmmo"}
function GM:HUDShouldDraw(name)
   for k, v in pairs(hud) do
      if name == v then return false end
   end

   return true
end